#ifndef REG_H
#define REG_H
#include <dtype.h>
//寄存器定义

//寄存器表
STRUCT_P(CHRT_REG)
{
    byte* CS;//当前代码指针
    //ptr ST;//栈顶指针
    int32 SI;//每访问一次从当前值+1
    int32 SD;//每访问一次从当前值-1
    ptr AP;//指针寄存器
    ptr BP;
    ptr CP;
    ptr DP;
    uint64 RV; //返回值寄存器
    union 
    {
        uint64 LAX;
        struct
        {
          union
          {
            uint32 AX;
            struct{
                uint16 AH;
                uint16 AL;
            };
          };
          union
          {
            uint32 BX;
            struct{
                uint16 BH;
                uint16 BL;
            };
          };
        };
    };
    union 
    {
        uint64 LBX;
        struct
        {
          union
          {
            uint32 CX;
            struct{
                uint16 CH;
                uint16 CL;
            };
          };
          union
          {
            uint32 DX;
            struct{
                uint16 DH;
                uint16 DL;
            };
          };
        };
    };
    uint64 LCX;
    uint64 LDX;
};

//寄存器类型
typedef enum {
    CHVM_REGTYPE_UNKNOWN = 0X00,//空
    CHVM_REGTYPE_CS = 0X01,//当前代码指针
    CHVM_REGTYPE_ST = 0X02,//栈顶指针
    CHVM_REGTYPE_SI = 0X03,//每访问一次从当前值+1
    CHVM_REGTYPE_SD = 0X04,//每访问一次从当前值-1
    CHVM_REGTYPE_RV = 0X05,//返回值寄存器
    CHVM_REGTYPE_AX = 0X90,//32位寄存器
    CHVM_REGTYPE_AH,//
    CHVM_REGTYPE_AL,//
    CHVM_REGTYPE_BX,//32位寄存器
    CHVM_REGTYPE_BH,//
    CHVM_REGTYPE_BL,//
    CHVM_REGTYPE_CX,//32位寄存器
    CHVM_REGTYPE_CH,//
    CHVM_REGTYPE_CL,//
    CHVM_REGTYPE_DX,//32位寄存器
    CHVM_REGTYPE_DH,//
    CHVM_REGTYPE_DL,//
    CHVM_REGTYPE_LAX,//64位寄存器，AX、BX共用高低位
    CHVM_REGTYPE_LBX,//64位寄存器，CX、DX共用高低位
    CHVM_REGTYPE_LCX,//64位寄存器
    CHVM_REGTYPE_LDX,//64位寄存器
    CHVM_REGTYPE_AP,//指针寄存器
    CHVM_REGTYPE_BP,//
    CHVM_REGTYPE_CP,//
    CHVM_REGTYPE_DP,//
}CHVM_REGTYPE;

#define Chvm_GetReg_CS(reg) (reg->CS)
//#define Chvm_GetReg_ST(reg) (reg->ST)
#define Chvm_GetReg_AP(reg) (reg->AP)
#define Chvm_GetReg_BP(reg) (reg->BP)
#define Chvm_GetReg_CP(reg) (reg->CP)
#define Chvm_GetReg_DP(reg) (reg->DP)
#define Chvm_GetReg_SI(reg) (reg->SI);(reg->SI+=1)
#define Chvm_GetReg_SD(reg) (reg->SD);(reg->SD-=1)
#define Chvm_GetReg_RV(reg) (reg->RV)
#define Chvm_GetReg_LAX(reg) (reg->LAX)
#define Chvm_GetReg_LBX(reg) (reg->LBX)
#define Chvm_GetReg_LCX(reg) (reg->LCX)
#define Chvm_GetReg_LDX(reg) (reg->LDX)
#define Chvm_GetReg_AX(reg) (reg->AX)
#define Chvm_GetReg_AH(reg) (reg->AH)
#define Chvm_GetReg_AL(reg) (reg->AL)
#define Chvm_GetReg_BX(reg) (reg->BX)
#define Chvm_GetReg_BH(reg) (reg->BH)
#define Chvm_GetReg_BL(reg) (reg->BL)
#define Chvm_GetReg_CX(reg) (reg->CX)
#define Chvm_GetReg_CH(reg) (reg->CH)
#define Chvm_GetReg_CL(reg) (reg->CL)
#define Chvm_GetReg_DX(reg) (reg->DX)
#define Chvm_GetReg_DH(reg) (reg->DH)
#define Chvm_GetReg_DL(reg) (reg->DL)

#define Chvm_SetReg_CS(reg,val) reg->CS = val
//#define Chvm_SetReg_ST(reg,val) reg->ST = val
#define Chvm_SetReg_AP(reg,val) reg->AP = val
#define Chvm_SetReg_BP(reg,val) reg->BP = val
#define Chvm_SetReg_CP(reg,val) reg->CP = val
#define Chvm_SetReg_DP(reg,val) reg->DP = val
#define Chvm_SetReg_SI(reg,val) reg->SI = val
#define Chvm_SetReg_SD(reg,val) reg->SD = val
#define Chvm_SetReg_RV(reg,val) reg->RV = val
#define Chvm_SetReg_LAX(reg,val) reg->LAX = val
#define Chvm_SetReg_LBX(reg,val) reg->LBX = val
#define Chvm_SetReg_LCX(reg,val) reg->LCX = val
#define Chvm_SetReg_LDX(reg,val) reg->LDX = val
#define Chvm_SetReg_AX(reg,val) reg->AX = val
#define Chvm_SetReg_AH(reg,val) reg->AH = val
#define Chvm_SetReg_AL(reg,val) reg->AL = val
#define Chvm_SetReg_BX(reg,val) reg->BX = val
#define Chvm_SetReg_BH(reg,val) reg->BH = val
#define Chvm_SetReg_BL(reg,val) reg->BL = val
#define Chvm_SetReg_CX(reg,val) reg->CX = val
#define Chvm_SetReg_CH(reg,val) reg->CH = val
#define Chvm_SetReg_CL(reg,val) reg->CL = val
#define Chvm_SetReg_DX(reg,val) reg->DX = val
#define Chvm_SetReg_DH(reg,val) reg->DH = val
#define Chvm_SetReg_DL(reg,val) reg->DL = val

#endif
